<?php

class TrainerProfileReviewController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'TrainerProfileReview'),
        ));
    }

    public function actioncreate() {
        $this->layout = false;
        $model = new TrainerProfileReview;
        if (isset($_POST['text'])) {
            Yii::app()->session['trainer_profile_review'] = $_POST;
            $loginuser = getLoginUserId();
            if (!empty($loginuser) && ($loginuser != $_POST['userId'])) {
                $model->setAttribute('reviews_text', $_POST['text']);
                $model->setAttribute('from_user_id', $loginuser);
                $model->setAttribute('create_time', SkillConfig::CreateDateTime());
                $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
                if ($_POST['userType'] == 'backend') {
                    $model->setAttribute('backend_user_id', $_POST['userId']);
                    $reviewsUser = $model->findByAttributes(array('to_user_id' => $_POST['userId'], 'backend_user_id' => $loginuser));

                    if ($reviewsUser <> "" && ($reviewsUser->backend_user_id == $loginuser)) {
                        echo 'againsameuser';
                        die;
                        unset(Yii::app()->session['trainer_profile_review']);
                    }
                } else {
                    $model->setAttribute('to_user_id', $_POST['userId']);
                    $reviewsUser = $model->findByAttributes(array('to_user_id' => $_POST['userId'], 'from_user_id' => $loginuser));

                    if ($reviewsUser <> "" && ($reviewsUser->from_user_id == $loginuser)) {
                        echo 'againsameuser';
                        die;
                        unset(Yii::app()->session['trainer_profile_review']);
                    }
                }
                if ($model->save(FALSE)) {
                    echo Message::TRAINER_SUCCESS_MSG;
                    unset(Yii::app()->session['trainer_profile_review']);
                } die;
            }

            if ($loginuser == $_POST['userId']) {
                echo 'fail';
                unset(Yii::app()->session['trainer_profile_review']);
            } else {
                echo 'redirect';
            }
        }
    }

    public function actiontrainerprofilereview() {
        $reviewMessage = '';
        if (isset($_GET['skillcode'])) {
            if ($_GET['skillcode'] == Message::success) {
                $reviewMessage = Message::TRAINER_SUCCESS_MSG;
            }
            if ($_GET['skillcode'] == Message::repeat_user) {
                $reviewMessage = 'You  have already made a query for this trainer.';
            }
            if ($_GET['skillcode'] == Message::owan_user) {
                $reviewMessage = "You can't give own review.";
            }
        }
        if (isset($_GET['type']) && isset($_GET['skillusers'])) {
            $traineruser = (isset($_GET['type']) && $_GET['type'] == 'frontend') ? 'to_user_id' : 'backend_user_id';
            $reviewdata = TrainerProfileReview::model()->findAllByAttributes(array($traineruser => $_GET['skillusers'], 'status' => SkillConfig::STATUS_ACTIVE));
            $this->render('_trainerProfilereview', array('reviewdata' => $reviewdata, 'reviewMessage' => $reviewMessage));
        } else {
            throw new CHttpException(403, 'You are not authorized to perform this action.');
        }
    }

}